package com.example.controller;

import com.example.pojo.Dept;
import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.service.DeptService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result selectAll() {
        List<Dept> depts = deptService.selectAll();
        return Result.success(depts);
    }

    @GetMapping("/{id}")
    public Result selectEmp(@PathVariable Integer id) {
        List<Emp> empList = deptService.selectEmp(id);
        return Result.success(empList);
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) {
        System.out.println(dept.getDepartmentName());
        deptService.add(dept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        deptService.delete(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        deptService.uodate(dept);
        return Result.success();
    }
}
